package blackstar.agent.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import blackstar.agent.AgentConfig;
import blackstar.agent.jvm.JMXProxyManager;
import blackstar.agent.jvm.JVMNameResolver;
import blackstar.agent.security.UserManager;

/**
 * @author RayWoo (ayufox@gmail.com)
 * 
 */
public class ServiceFactory
{
	private static final String JMX_PROXY_MANAGER = "jmxProxyManager";

	private static final String JVM_NAME_RESOLVER = "jvmNameResolver";

	private static final String USER_MANAGER = "userManager";

	private final static Log LOGGER = LogFactory.getLog(ServiceFactory.class);

	private static Map<String, Object> services = new HashMap<String, Object>();
	static
	{
		LOGGER.info("init default services");
		String userManagerClassName = AgentConfig
				.getProperty("security.UserManager");
		if (userManagerClassName != null
				&& userManagerClassName.trim().length() > 0)
		{
			LOGGER.info("UserManager[" + userManagerClassName + "] Found");
			try
			{
				UserManager userManager = (UserManager) Class.forName(
						userManagerClassName.trim()).newInstance();
				regist(USER_MANAGER, userManager);
			} catch (Exception e)
			{
				LOGGER.error("Create UserManager[" + userManagerClassName
						+ "] Error", e);
				throw new IllegalArgumentException("Create UserManager["
						+ userManagerClassName + "] Error", e);
			}
		} else
		{
			LOGGER.info("UserManager Config Not Found");
		}

		String jvmNameResolverClassName = AgentConfig
				.getProperty("JVMNameResolver");
		if (jvmNameResolverClassName != null
				&& jvmNameResolverClassName.trim().length() > 0)
		{
			LOGGER.info("JVMNameResolver[" + jvmNameResolverClassName
					+ "] Found");
			try
			{
				JVMNameResolver jvmNameResolver = (JVMNameResolver) Class.forName(
						jvmNameResolverClassName.trim()).newInstance();
				regist(JVM_NAME_RESOLVER, jvmNameResolver);
			} catch (Exception e)
			{
				LOGGER.error("Create JVMNameResolver["
						+ jvmNameResolverClassName + "] Error", e);
				throw new IllegalArgumentException("Create JVMNameResolver["
						+ jvmNameResolverClassName + "] Error", e);
			}
		} else
		{
			LOGGER.info("JVMNameResolver Config Not Found");
		}
		LOGGER.info("init default services success");
	}

	public static void regist(String name, Object service)
	{
		services.put(name, service);
	}

	public static Object getService(String name)
	{
		return services.get(name);
	}

	public static <T> T getService(String name, Class<T> type)
	{
		return (T) services.get(name);
	}

	public static UserManager getUserManager()
	{
		return getService(USER_MANAGER, UserManager.class);
	}

	public static JVMNameResolver getJVMNameResolver()
	{
		return getService(JVM_NAME_RESOLVER, JVMNameResolver.class);
	}

	public static void registJMXProxyManager(JMXProxyManager jmxProxyManager)
	{
		regist(JMX_PROXY_MANAGER, jmxProxyManager);
	}

	public static JMXProxyManager getJMXProxyManager()
	{
		return getService(JMX_PROXY_MANAGER, JMXProxyManager.class);
	}
}
